<template>
  <div class="permission-container">
    <div style="margin-bottom: 20px">
      <el-button type="primary" @click="addPermissionTmp(1, 0)">添加权限</el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        prop="name"
        label="名称"
      />
      <el-table-column
        prop="code"
        label="标识"
      />
      <el-table-column
        prop="description"
        label="描述"
      />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button type="text" @click="addPermissionTmp(2, row.id)">添加</el-button>
          <el-button type="text" @click="editPermissionTmp(row.id)">编辑</el-button>
          <el-button type="text" @click="deletePermisstionTmp(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <PermissionDialog v-if="permissionDialogVisible" v-model="permissionDialogVisible" :son-data="sonData" :edit-id="editId" @success="getPermissionList" /> -->
    <!-- <PermissionDialog :key="countKey" v-model="permissionDialogVisible" :son-data="sonData" :edit-id="editId" @success="getPermissionList" /> -->
    <PermissionDialog v-model="permissionDialogVisible" :son-data="sonData" :edit-id="editId" @success="getPermissionList" />
  </div>
</template>

<script>
import { getPermissionListRequest, deletePermissionByIdRequest } from '@/api/permission'
import { transfromListToTree } from '@/utils'
import PermissionDialog from './components/PermissionDialog.vue'

export default {
  name: 'PermissionCom',
  components: { PermissionDialog },
  data() {
    return {
      tableData: [],
      permissionDialogVisible: false,
      sonData: {
        pid: null,
        type: null
      },
      editId: null,
      countKey: 1
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionListRequest()
      // 平铺数据转换为tree树形数据--- 带有children
      this.tableData = transfromListToTree(res.data, 0)
    },
    addPermissionTmp(type, pid) {
      this.permissionDialogVisible = true
      this.countKey++
      this.sonData.type = type
      this.sonData.pid = pid
    },
    // 编辑的函数
    editPermissionTmp(id) {
      this.permissionDialogVisible = true
      this.countKey++
      // 当我打开弹出框，子组件不会重新渲染，只会将当前数据更新以后，由父组件传递给子组件
      // 子组件怎么样会重新渲染？ v-if
      this.editId = id
      // this.$nextTick(() => {
      //   this.$refs.comRef.子组件的方法()
      // })
    },
    // 删除
    deletePermisstionTmp(id) {
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deletePermissionByIdRequest(id)
        this.getPermissionList()
      }).catch(() => {
        //
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.permission-container {
  padding: 20px;
}
</style>
